function MyEvent(){
  // 准备一个数据结构用于缓存订阅者信息
  this._events = Object.create(null)
}

MyEvent.prototype.on = function (type, callback) {
  // 判断当前事件监听是否存在，然后再决定如何缓存
  if(this._events[type]){
    this._events[type].push(callback)
  }else{
    this._events[type] = [callback]
  }
}

MyEvent.prototype.emit = function (type, ...args) {
  if(this._events&&this._events[type].length){
    this._events[type].forEach((callback)=>{
      callback.call(this,...args)
    })
  }
}

MyEvent.prototype.off = function (type, callback) {
  // 判断当前事件监听是否存在，然后存在则取消指定的监听
  if(this._events&&this._events[type].length){
    for(var i=this._events[type].length-1;i>-1;i--){
      if(this._events[type][i]===callback||this._events[type][i].link===callback){
        this._events[type].splice(i,1);
      }
    }
  }
}

MyEvent.prototype.once = function (type, callback) {
  let tempFn = function (...args){
    callback.call(this,...args)
    this.off(type,tempFn)
  }
  tempFn.link = callback
  this.on(type,tempFn)
}

let ev = new MyEvent();

let fn = function(...args){
console.log('事件1执行了', args)
}
// ev.on('事件1', fn)
// ev.on('事件1', function(){
//   console.log('事件1第二次执行')
// })
// ev.emit('事件1',1,2,3)
// ev.off('事件1',fn)
// ev.emit('事件1',1)

ev.once('事件1', fn)
ev.off('事件1', fn)
ev.emit('事件1',1,2,3)
ev.emit('事件1',1)
